# -*- coding: utf-8 -*-
import scrapy
from csdn.items import CsdnItem
from scrapy_redis.spiders import RedisSpider
import re

class CoursesSpider(RedisSpider):
    name = 'courses'
    #allowed_domains = ['edu.csdn.net']
    #start_urls = ['http://edu.csdn.net/']
    redis_key = 'courses:start_urls'

    def __init__(self,*args,**kwargs):
        domain = kwargs.pop('domain','')
        self.allowed_domains = filter(None,domain.split(','))
        super(CoursesSpider,self).__init__(*args,**kwargs)

    def parse(self, response):
        item = CsdnItem()
        res = response.selector.css('div.main')
        item['title'] = res.css('div.info_right h1::text').extract_first()
        item['professor_name'] = res.css('div.professor_name a::text').extract_first()
        item['course_status'] = res.css('div.course_status span::text').extract_first()
        item['price'] = res.re('<sapn class="money">(.*?)</sapn>')[0]
        outlinelists = res.css('dl.outline_list')
        outline = ""
        for outlinelist in outlinelists:
            outline += outlinelist.css('dt span::text').extract_first().strip()+'\n'
        item['outline'] = outline
        print(item)
        yield item


